<template>
    <main class="login-wrap">
        <div class="main-login-wrap">
            <div class="wp flex jc-fe">
                <div class="login-form-card">
                   <!-- <div class="none-input">
                        <input type="text">
                        <input type="password">
                    </div>-->
                    <!--账号登陆-->
                    <div v-if="loginType === 'account'">
                        <!--<img class="qr-fix"
                             src="../../assets/login/qr-fix.png"
                             alt=""
                             @click="handlerToggleLoginType">-->
                        <div class="title">账号登录</div>
                        <div class="help-text">
                            <span>没有账号？</span>
                            <router-link to="/register">立即注册</router-link>
                        </div>
                        <label class="input">
                            <img class="icon"
                                 style="width: 20px;"
                                 src="../../assets/icon/icon-username.png" alt="">
                            <input type="text"
                                   v-model="form.username"
                                   :placeholder="accountLoginTypeCode ? '国内手机号' : '国内手机号/用户名/邮箱/卡号'">
                        </label>
                        <label class="input" v-if="accountLoginTypeCode">
                            <img class="icon"
                                 style="width: 18px;"
                                 src="../../assets/icon/icon-password.png" alt="">
                            <input type="text" v-model="form.verificationCode" placeholder="请输入动态验证码">
                            <a href="javascript:;"
                               class="link cur"
                               :class="{disabled:yzmBtn.disabled}"
                               @click="sendYzm">{{yzmBtn.text}}</a>
                        </label>
                        <label class="input" v-else>
                            <img class="icon"
                                 style="width: 18px;"
                                 src="../../assets/icon/icon-password.png" alt="">
                            <input type="password"
                                   v-model="form.password"
                                   placeholder="请输入您的密码">
                            <router-link to="/password" class="link">忘记密码？</router-link>
                        </label>

                        <div class="other-operate flex al-c jc-sb">
                            <!--<i-checkbox> 30天内自动登录</i-checkbox>-->
                            <span></span>
                            <div class="flex al-c cur"
                                 @click="accountLoginTypeCode = !accountLoginTypeCode">
                                <span v-if="!accountLoginTypeCode">手机动态密码登录</span>
                                <span v-else>账号登录</span>
                                <icon type="md-arrow-dropright-circle"/>
                            </div>
                        </div>
                        <button type="primary" :loading="loading" long @click="handlerLogin">登陆</button>
                        <div class="help-text xieyi">
                            登录即代表您同意我们的
                            <a href="/xieyi.html" target="_blank">《服务协议和隐私政策》</a>
                        </div>
                        <!--<div class="other-login-icon">
                            <img src="../../assets/icon/icon-baidu.png" alt="">
                            <img src="../../assets/icon/icon-qq.png" alt="">
                            <img src="../../assets/icon/icon-sina.png" alt="">
                            <img src="../../assets/icon/icon-weixin.png" alt="">
                            <img src="../../assets/icon/icon-zfb.png" alt="">
                        </div>-->
                    </div>
                    <!--扫码登陆-->
                    <div v-if="loginType === 'qrcode'">
                        <img class="qr-fix"
                             src="../../assets/login/account-fix.png"
                             alt=""
                             @click="handlerToggleLoginType">
                        <div class="title">扫码登录</div>
                        <div>
                            <img class="qrcode" src="https://s2.ax1x.com/2019/08/27/moMSQU.png" alt="">
                        </div>
                        <div class="text-center qrcode-text">
                            打开$TEXT$APP或微信“扫一扫” <br>
                            扫描二维码登录
                        </div>
                    </div>
                </div>
            </div>
            <div class="l-footer">
            </div>
        </div>
    </main>
</template>

<script>
    import yzm from '@/mixins/yzm'

    export default {
        name: 'Login',
        mixins: [yzm],
        data() {
            return {
                loginType: 'account',
                accountLoginTypeCode: false,
                loading: false,
                form: {
                    username: '',
                    password: '',
                    // username: '15733125234',
                    // password: '111111',
                    verificationCode: '',
                }
            }
        },
        computed: {},
        async created() {
        },
        async mounted() {
        },
        components: {},
        methods: {
            handlerToggleLoginType() {
                if (this.loginType === 'account') {
                    this.loginType = 'qrcode'
                } else {
                    this.loginType = 'account'
                }
            },
            async handlerLogin() {
                this.loading = true
                try {
                    let token = ''
                    if (this.loginType === 'account') {
                        if (this.accountLoginTypeCode) {
                            const {data} = await this.$api.user.loginByCode(this.form.username, this.form.verificationCode)
                            token = data
                        } else {
                            const {data} = await this.$api.user.login(this.form.username, this.form.password)
                            token = data
                        }
                    }
                    this.$utils.store.set('token', token)
                    await this.$root.getUserInfo()
                    if (window.history.length === 1) {
                        this.$router.replace('/')
                    } else {
                        this.$router.go(-1)
                    }
                } catch (e) {
                    console.error(e)
                }
                this.loading = false
            },
            async sendYzm() {
                if (this.yzmBtn.disabled) {
                    return
                }
                const phone = this.form.username
                if (phone.trim().length !== 11) {
                    this.$Message.error('请输入手机号')
                    return
                }
                try {
                    await this.$api.user.sendMessage(phone)
                    this.getYzmFn()
                } catch (e) {
                    console.error(e)
                }
            },
        }
    }
</script>

<style lang="scss">
    @import "../../../../style/var";
    @import "../../../../style/mixin";

    .login-wrap {
        background-color: #fff;

        .none-input {
            position: absolute;
            width: 0;
            height: 0;
            border: none;
            overflow: hidden;
        }

        .l-header {
            height: 110px;
        }

        .l-footer {
            width: 100%;
            height: 80px;
            text-align: center;
            padding-top: 30px;
            color: #666;
            font-size: 14px;
        }

        .main-login-wrap {
            min-height: calc(100vh - 110px);
            width: 100%;
            background: #dff1ff url("../../assets/login/login-bg.png") no-repeat top center;
            background-size: 100%;
            padding: 70px 0 1px;
            display: flex;
            flex-direction: column;
            align-items: flex-end;
            justify-content: space-between;
        }

        .login-form-card {
            position: relative;
            width: 460px;
            min-height: 400px;
            border-radius: 15px;
            background-color: #fff;
            padding: 50px 35px 0;
            overflow: hidden;

            .qr-fix {
                position: absolute;
                top: -1px;
                right: -1px;
                cursor: pointer;
                width: 65px;
                display: block;
            }

            .qrcode {
                width: 150px;
                display: block;
                margin: 60px auto 30px;
            }

            .qrcode-text {
                color: #5c5c5c;
                font-size: 16px;
            }

            .title {
                font-size: 24px;
                color: #454545;
                margin-bottom: 15px;
            }

            .help-text {
                text-align: right;
                color: #a4a4a4;
                font-size: 14px;
                margin-bottom: 12px;

                a {
                    color: $link-color;
                }
            }

            .xieyi {
                text-align: left;
                color: #a8a8a8;
                margin-top: 10px;

                a {
                    color: #7c7c7c;
                    text-decoration: underline;
                }
            }

            .other-login-icon {
                display: flex;
                align-items: flex-end;
                padding-top: 10px;

                img {
                    width: 28px;
                    display: block;
                    margin-right: 15px;
                    cursor: pointer;
                    opacity: .8;

                    &:hover {
                        opacity: 1;
                    }
                }
            }

            .input {
                position: relative;
                display: flex;
                margin-bottom: 20px;
                height: 40px;

                .icon {
                    position: absolute;
                    display: block;
                    left: 12px;
                    top: 50%;
                    transform: translateY(-50%);
                }

                .link {
                    @extend .icon;
                    left: auto;
                    right: 12px;
                    color: $link-color;
                    font-size: 14px;
                    text-decoration: underline;
                }

                input {
                    width: 100%;
                    border-radius: 5px;
                    border: 1px solid #d9d9d9;
                    outline: none;
                    font-size: 14px;
                    padding-left: 45px;
                }
            }

            .other-operate {
                line-height: 1;
                margin-bottom: 20px;
                font-size: 14px;
                color: #7a7878;

                .ivu-icon {
                    font-size: 16px;
                    margin-left: 5px;
                }

                span {
                    vertical-align: middle;
                }

                .ivu-checkbox-inner {
                    width: 16px;
                    height: 16px;

                    &:after {
                        top: 2px;
                    }
                }
            }
        }
    }
</style>
